package com.mys.shorturl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;



public class GetShortUrlDao {
	
	//HttpServletRequest request;
	
	
	//ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
	
//public GetShortUrlDao(){
//		
//	}
//
//	public GetShortUrlDao(HttpServletRequest request) {
//		
//		this.request = request;
//	}
	
	
public String getMyurl(HttpServletRequest request){
		
		ServletContext servletContext = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		Params params = (Params) ac.getBean("ParamsCode");
		String shortLogin = params.getLogin();
		String shortKey = params.getKey();
		String shortFormat = params.getFormat();
		
		String response = null;
		String longUrl = "http://news.163.com/11/0711/00/78L2HFKH00014JB6.html";
		try {
			String requestUrl = URLEncoder.encode(longUrl,"UTF-8");
			String fullRequest = "http://api.bitly.com/v3/shorten?login="+shortLogin+"&apiKey="+shortKey+"&longUrl="+requestUrl+"&format="+shortFormat;
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(fullRequest);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			response = httpClient.execute(httpget, responseHandler);
			httpClient.getConnectionManager().shutdown();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
		
	}
	
	
	


	public String getShortUrl(HttpServletRequest request,String longUrl){
		
		ServletContext servletContext = request.getSession().getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		Params params = (Params) ac.getBean("ParamsCode");
		String shortLogin = params.getLogin();
		String shortKey = params.getKey();
		String shortFormat = params.getFormat();
		
		String response = null;
		//String shortUrl = "http://news.163.com/11/0711/00/78L2HFKH00014JB6.html";
		try {
			String requestUrl = URLEncoder.encode(longUrl,"UTF-8");
			String fullRequest = "http://api.bitly.com/v3/shorten?login="+shortLogin+"&apiKey="+shortKey+"&longUrl="+requestUrl+"&format="+shortFormat;
			//System.out.println(fullRequest);
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(fullRequest);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			response = httpClient.execute(httpget, responseHandler);
			httpClient.getConnectionManager().shutdown();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
		
	}

	


}
